package com.bjsasc.open.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.tomcat.util.codec.binary.Base64;

public class AESUtil {

	 public static final String KEY = "1234567890hijklm";

	// 偏移量，AES 128位数据块对应偏移量为16位
	 public static final String VIPARA = "1234567890abcdef"; //AES
	// 128位数据块对应偏移量为16位

	// AES：加密方式 CBC：工作模式 PKCS5Padding：填充模式
	private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";

	private static final String AES = "AES";

	// 编码方式
	public static final String CODE_TYPE = "UTF-8";

	/**
	 * AES 加密操作
	 *
	 * @param content
	 *            待加密内容
	 * @param key
	 *            加密密钥
	 * @return 返回Base64转码后的加密数据
	 * @throws Exception 
	 */
	public static String encrypt(String content, String key, String vipara) throws Exception {

		if (content == null || "".equals(content)) {
			return content;
		}

		try {
			/*
			 * 新建一个密码编译器的实例，由三部分构成，用"/"分隔，分别代表如下 1. 加密的类型(如AES，DES，RC2等) 2.
			 * 模式(AES中包含ECB，CBC，CFB，CTR，CTS等) 3. 补码方式(包含nopadding/PKCS5Padding等等)
			 * 依据这三个参数可以创建很多种加密方式
			 */
			Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);

			// 偏移量
			IvParameterSpec zeroIv = new IvParameterSpec(vipara.getBytes(CODE_TYPE));

			byte[] byteContent = content.getBytes(CODE_TYPE);

			// 使用加密秘钥
			SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(CODE_TYPE), AES);
			// SecretKeySpec skeySpec = getSecretKey(key);

			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, zeroIv);// 初始化为加密模式的密码器

			byte[] result = cipher.doFinal(byteContent);// 加密

			return Base64.encodeBase64String(result);// 通过Base64转码返回
		} catch (Exception ex) {
			throw new Exception("encrypt failed", ex);
		}

	}

	/**
	 * AES 解密操作
	 *
	 * @param content
	 * @param key
	 * @return
	 * @throws Exception 
	 */
	public static String decrypt(String content, String key, String vipara) throws Exception {
		if (content == null || "".equals(content)) {
			return content;
		}

		try {
			// 实例化
			Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
			IvParameterSpec zeroIv = new IvParameterSpec(vipara.getBytes(CODE_TYPE));

			SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(CODE_TYPE), AES);
			// SecretKeySpec skeySpec = getSecretKey(key);
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, zeroIv);

			byte[] result = cipher.doFinal(Base64.decodeBase64(content));

			return new String(result, CODE_TYPE);
		} catch (Exception ex) {
			throw new Exception("decrypt failed", ex);
		}

	}

	public static void main(String[] args) throws Exception {
		String content = "bjsasc12";
		/*String key = "1234567890hijklm";
		String vipara = "1234567890abcdef";*/
		System.out.println("content:" + content);
		String s1 = AESUtil.encrypt(content, KEY, VIPARA);
		System.out.println("加密后的值为:" + s1);
		System.out.println("解密后的值为:" + AESUtil.decrypt("H9srFzJy0+3f9X9HGkUfug==", KEY, VIPARA));

	}

}
